package com.sc.syllabus.money;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sc.syllabus.R;
import com.srain.utils.LogUtils;

import java.util.List;

/**
 * Created by liushuanglong on 2017/9/13.
 */
public class MoneyMainActivity extends Activity {

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MoneyMainActivity.class);
        context.startActivity(intent);
    }

    private static final String TAG = "MoneyMainActivity";
    private ListView listView;
    private TextView textView;
    private MoneyModelImpl mModel;
    private MoneyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money);
        mModel = new MoneyModelImpl(this);
        init();
    }

    private void init() {
        listView = (ListView) findViewById(R.id.listview);
        textView = (TextView) findViewById(R.id.text);
        textView.setOnClickListener(mClickListener);
        listView.setOnItemClickListener(mItemClickListener);
        List<MoneyInfo> beans = mModel.queryAll();
        if (null != beans && !beans.isEmpty()) {
            adapter = new MoneyAdapter(MoneyMainActivity.this, beans);
            listView.setAdapter(adapter);
        } else {
            textView.setText("Beans is empty!!!");
            LogUtils.e(TAG, "beans is empty!!!");
        }
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.text:
                    MoneyEditActivity.actionStart(MoneyMainActivity.this);
                    break;
            }
        }
    };

    private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MoneyInfo bean = adapter.getItem(position);
            MoneyEditActivity.actionStart(MoneyMainActivity.this, bean);
        }
    };
}
